| Total Complexity | 2 |
| Total Lines | 15 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { Injectable, CanActivate, ExecutionContext} from '@nestjs/common'; |
||
| 3 | |||
| 4 | @Injectable() |
||
| 5 | export class RolesGuard implements CanActivate { |
||
| 6 | constructor(private reflector: Reflector) {} |
||
| 7 | |||
| 8 | public canActivate(context: ExecutionContext): boolean { |
||
| 9 | const roles = this.reflector.get<string[]>('roles', context.getHandler()); |
||
| 10 | |||
| 11 | if (!roles) { |
||
| 12 | return true; |
||
| 13 | } |
||
| 14 | |||
| 15 | const request = context.switchToHttp().getRequest(); |
||
| 16 | |||
| 17 | return roles.includes(request.user.role); |
||
| 18 | } |
||
| 20 |